#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>

int main()
{
    pid_t id = fork();
    if (id < 0)
    {
        perror("fork");
        return errno;
    }
    else if (id == 0)
    {
        // child
        int cnt = 3;
        while (cnt)
        {
            printf("child, pid: %d, ppid: %d, cnt: %d\n", getpid(), getppid(), cnt);
            cnt--;
            sleep(1);
        }
        exit(0);
    }
    else 
    {
        //parent
        int cnt = 6;
        while (cnt)
        {
            printf("parent, pid: %d, ppid: %d, cnt: %d\n", getpid(), getppid(), cnt);
            cnt--;
            sleep(1);
        }
        pid_t ret = wait(NULL);
        if  (ret == id) // 父进程拿到的id就是子进程的pid
        {
            printf("wait success, ret: %d\n", ret);
        }

        sleep(3);
    }
    return 0;
}


